package com.ship.boats.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoatsPscInspect;
import com.ship.boats.domain.dto.ExportInspectionsRq;
import com.ship.boats.domain.dto.ExportPscInspectRq;
import com.ship.boats.domain.dto.QueryPscInspectRq;
import com.ship.boats.domain.vo.*;
import com.ship.boats.service.SpBoatsPscInspectService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.base.Page;
import com.ship.common.core.utils.poi.ExcelUtil;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <b>SpBoatsPscInspectController</b>
 *
 * @description: SpBoatsPscInspectController <br>
 * @date: 2024/3/21 09:55 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "PSC检查模块")
@RestController
public class SpBoatsPscInspectController {
    private static final Logger logger = LoggerFactory.getLogger(SpBoatsPscInspectController.class);

    @Autowired
    private SpBoatsPscInspectService spBoatsPscInspectService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @ApiOperation("查询PSC检查查询条件列表")
    @GetMapping("/query/psc_inspect_type/list")
    public JsonResult queryPscInspectTypeList(@RequestParam String queryType){
        List<String> returnList = spBoatsPscInspectService.queryPscInspect(queryType);
        return JsonResult.success(returnList);
    }

    @ApiOperation("分页查询PSC检查记录列表")
    @PostMapping("/query/psc_inspect/list")
    public JsonResult queryPscInspectList(@RequestBody QueryPscInspectRq rq){
        logger.info("分页查询PSC检查记录列表入参:{}"+ JSONUtil.parse(rq));
        PageInfo<QueryPscInspectInfo> pageInfo = spBoatsPscInspectService.queryPscInspectList(rq);
        logger.info("分页查询PSC检查记录列表返回结果:{}"+JSONUtil.parse(pageInfo));
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("根据psc检查ID查询psc检查详情")
    @GetMapping("/query/psc_inspect/byId")
    public JsonResult queryPscInspectById(@RequestParam Long id){
        QueryInspectionDetailsInfo inspectionDetails = spBoatsPscInspectService.queryPscInspectById(id);
        logger.info("根据psc检查ID"+id+"查询psc检查详情返回结果:{}"+JSONUtil.parse(inspectionDetails));
        return JsonResult.success(inspectionDetails);
    }

    @ApiOperation("导出PSC检查记录")
    @PostMapping("/export/psc_inspect")
    public JsonResult exportPSCInspect(HttpServletResponse response,
                                       @RequestBody ExportPscInspectRq rq){
        logger.info("导出psc检查记录入参:{}"+JSONUtil.parse(rq));
        List<QueryPscInspectInfo> pscInspectList = spBoatsPscInspectService.exportPscInspect(rq);
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("shipName");
        tableNameList.add("imo");
        tableNameList.add("authorisation");
        tableNameList.add("inspectionsType");
        tableNameList.add("inspectionDate");
        tableNameList.add("inspectionPortDecode");
        tableNameList.add("numberOfDefects");
        tableNameList.add("numberOfDaysDetained");
        tableNameList.add("lastUpdated");
        tableNameList.add("numberOfPartDaysDetained");
        tableNameList.add("releaseDate");
        tableNameList.add("shipDetained");
        tableNameList.add("source");
        List<String> tableList = new ArrayList<>();
        tableList.add("船舶名称");
        tableList.add("IMO");
        tableList.add("检测机构");
        tableList.add("检测类型");
        tableList.add("检测时间");
        tableList.add("检测港口名称");
        tableList.add("缺陷数量");
        tableList.add("整船滞留时间");
        tableList.add("最后更新时间");
        tableList.add("部分零件滞留天数");
        tableList.add("结束检查日期");
        tableList.add("船舶是否扣押");
        tableList.add("来源");
        Field[] fields = QueryPscInspectInfo.class.getDeclaredFields();
        String fileName = null;
        if (CollectionUtil.isNotEmpty(rq.getImoList()) && rq.getImoList().size() == 1){
            fileName = pscInspectList.get(0).getShipName()+"PSC检查记录";
        }else {
            fileName = "PSC检查记录";
        }
        logger.info("文件名称："+fileName);
        String url = exportDownloadUtil.exportUtil(pscInspectList,fields, tableNameList,tableList, fileName);

        return JsonResult.success(url);
    }

    @ApiOperation("导出证书记录")
    @PostMapping("/export/inspections_certificates")
    public JsonResult exportInspectionsCertificates(HttpServletResponse response,
                                                    @RequestBody ExportInspectionsRq rq){
        //根据检查ID查询船舶信息
        SpBoatsPscInspect spBoatsPscInspect = spBoatsPscInspectService.queryBoatsInspect(rq.getInspectionId());
        List<QueryInspectionsCertificatesInfo> certificatesList = spBoatsPscInspectService.InspectionsCertificatesList(rq);
        Field[] fields = QueryInspectionsCertificatesInfo.class.getDeclaredFields();
        String url = exportDownloadUtil.pageExport(certificatesList,fields,
                exportDownloadUtil.listStrByModuleAndUser("sp_inspections_certificates",
                        SecurityUtils.getUserId()),
                spBoatsPscInspect.getShipName()+"-"+spBoatsPscInspect.getImo()+"-检测证书-");
        return JsonResult.success(url);
    }

    @ApiOperation("导出缺陷记录信息")
    @PostMapping("/export/inspections_defects")
    public JsonResult exportInspectionsDefects(HttpServletResponse response,@RequestBody ExportInspectionsRq rq){
        //根据检查ID查询船舶信息
        SpBoatsPscInspect spBoatsPscInspect = spBoatsPscInspectService.queryBoatsInspect(rq.getInspectionId());
        List<QueryInspectionsDefectsInfo> defectsList = spBoatsPscInspectService.queryInspectionsDefectsList(rq);
        Field[] fields = QueryInspectionsDefectsInfo.class.getDeclaredFields();
        String url = exportDownloadUtil.pageExport(defectsList,fields,exportDownloadUtil.listStrByModuleAndUser("sp_inspections_defects", SecurityUtils.getUserId()), spBoatsPscInspect.getShipName()+"-"+spBoatsPscInspect.getImo()+"-缺陷列表-");
        return JsonResult.success(url);
    }
}
